<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><%= title %> - 彼知AI</title>
    <meta name="description" content="<%= description || '彼知AI数据预览' %>">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'Helvetica Neue', Helvetica, Arial, sans-serif;
            line-height: 1.6;
            color: #333;
            background-color: #f5f5f5;
            min-height: 100vh;
            display: flex;
            flex-direction: column;
        }

        .preview-header {
            background: white;
            border-bottom: 1px solid #e0e0e0;
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
            position: sticky;
            top: 0;
            z-index: 100;
        }

        .header-content {
            max-width: 1200px;
            margin: 0 auto;
            padding: 0 24px;
            height: 64px;
            display: flex;
            align-items: center;
            justify-content: space-between;
        }

        .app-title {
            font-size: 20px;
            font-weight: 600;
            color: #333;
            text-decoration: none;
        }

        .app-title:hover {
            color: #667eea;
        }

        .header-actions {
            display: flex;
            align-items: center;
            gap: 16px;
        }

        .btn {
            padding: 8px 16px;
            border: 1px solid #d9d9d9;
            border-radius: 6px;
            background: white;
            color: #333;
            text-decoration: none;
            font-size: 14px;
            transition: all 0.2s;
            cursor: pointer;
        }

        .btn:hover {
            border-color: #667eea;
            color: #667eea;
        }

        .preview-main {
            flex: 1;
            padding: 0;
            overflow: hidden;
        }

        .content-container {
            width: 100%;
            height: 100%;
            padding: 24px;
            margin: 0;
            max-width: none;
        }

        .preview-footer {
            background: white;
            border-top: 1px solid #e0e0e0;
            padding: 16px 0;
        }

        .footer-content {
            max-width: 1200px;
            margin: 0 auto;
            padding: 0 24px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            color: #666;
            font-size: 14px;
        }

        .footer-left {
            flex: 1;
        }

        .footer-right {
            flex: 0 0 auto;
        }

        .api-selector {
            display: flex;
            align-items: center;
            gap: 8px;
            font-size: 12px;
        }

        .api-label {
            color: #666;
            white-space: nowrap;
        }

        .api-select {
            padding: 4px 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 12px;
            background: white;
            min-width: 140px;
        }

        .api-test-btn {
            padding: 4px 12px;
            background: #17a2b8;
            color: white;
            border: none;
            border-radius: 4px;
            font-size: 12px;
            cursor: pointer;
            white-space: nowrap;
        }

        .api-test-btn:hover {
            background: #138496;
        }

        .api-test-btn:disabled {
            background: #6c757d;
            cursor: not-allowed;
        }

        .api-status {
            padding: 4px 8px;
            border-radius: 4px;
            font-size: 11px;
            display: none;
            white-space: nowrap;
            max-width: 200px;
        }

        .card {
            background: white;
            border-radius: 12px;
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
            margin-bottom: 24px;
            overflow: hidden;
        }

        .card-header {
            padding: 20px 24px;
            border-bottom: 1px solid #f0f0f0;
            background: #fafafa;
        }

        .card-title {
            font-size: 18px;
            font-weight: 600;
            color: #333;
            margin: 0;
        }

        .card-content {
            padding: 24px;
        }

        .tag {
            display: inline-block;
            padding: 4px 12px;
            border-radius: 16px;
            font-size: 12px;
            font-weight: 500;
            margin-right: 8px;
            margin-bottom: 8px;
        }

        .tag-primary {
            background: #e6f3ff;
            color: #0066cc;
        }

        .tag-success {
            background: #e6f7e6;
            color: #00b300;
        }

        .tag-warning {
            background: #fff3e0;
            color: #ff8c00;
        }

        .tag-error {
            background: #ffe6e6;
            color: #cc0000;
        }

        .avatar {
            width: 60px;
            height: 60px;
            border-radius: 50%;
            object-fit: cover;
            border: 3px solid #fff;
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
        }

        .avatar-large {
            width: 120px;
            height: 120px;
        }

        .text-content {
            background: #f8f9fa;
            border-radius: 8px;
            padding: 20px;
            border-left: 4px solid #667eea;
            margin: 16px 0;
        }

        .text-content p {
            margin: 0;
            line-height: 1.8;
            white-space: pre-wrap;
            word-break: break-word;
        }

        .error-content {
            border-left-color: #dc3545;
            background: #fff5f5;
        }

        .error-content p {
            color: #dc3545;
        }

        .loading {
            text-align: center;
            padding: 60px 20px;
            color: #666;
        }

        .error {
            text-align: center;
            padding: 60px 20px;
        }

        .error h2 {
            color: #dc3545;
            margin-bottom: 16px;
        }

        .error p {
            color: #666;
            margin-bottom: 24px;
        }

        /* 响应式设计 */
        @media (max-width: 768px) {
            .header-content,
            .content-container,
            .footer-content {
                padding: 0 16px;
            }
            
            .app-title {
                font-size: 18px;
            }
            
            .preview-main {
                padding: 16px 0;
            }

            .card-content {
                padding: 16px;
            }

            .footer-content {
                flex-direction: column;
                gap: 12px;
                text-align: center;
            }

            .api-selector {
                flex-wrap: wrap;
                justify-content: center;
                gap: 6px;
            }

            .api-select {
                min-width: 120px;
            }

            .api-status {
                max-width: 100%;
                text-align: center;
            }
        }
    </style>

    <!-- API测试相关JavaScript -->
    <script>
        // API测试功能
        document.addEventListener('DOMContentLoaded', function() {
            const apiModeSelect = document.getElementById('apiMode');
            const testApiBtn = document.getElementById('testApiBtn');
            const apiStatus = document.getElementById('apiStatus');

            console.log('Layout DOM加载完成');
            console.log('apiModeSelect:', apiModeSelect);
            console.log('testApiBtn:', testApiBtn);
            console.log('apiStatus:', apiStatus);

            // 测试API连接
            async function testApiConnection() {
                const mode = apiModeSelect.value;
                testApiBtn.disabled = true;
                testApiBtn.textContent = '测试中...';

                let results = [];

                try {
                    if (mode === 'remote') {
                        // 测试远程API
                        const response = await fetch('http://47.106.80.190:7882/health', {
                            method: 'GET',
                            mode: 'cors',  // 明确指定CORS模式
                            credentials: 'omit'  // 不发送凭据
                        });

                        if (response.ok) {
                            results.push('✅ 远程API (7882端口) - 连接正常');
                        } else {
                            results.push('❌ 远程API (7882端口) - 服务异常');
                        }
                    }
                } catch (error) {
                    console.error('API连接测试失败:', error);
                    if (error.name === 'TypeError' && error.message.includes('Failed to fetch')) {
                        results.push('❌ 远程API (7882端口) - CORS跨域错误或网络不可达');
                    } else {
                        results.push(`❌ 远程API (7882端口) - 连接失败: ${error.message}`);
                    }
                }

                // 显示结果
                if (results.length > 0) {
                    apiStatus.innerHTML = results.join('<br>');
                    apiStatus.style.display = 'block';
                    apiStatus.style.background = results.some(r => r.includes('❌')) ? '#f8d7da' : '#d4edda';
                    apiStatus.style.color = results.some(r => r.includes('❌')) ? '#721c24' : '#155724';
                    apiStatus.style.border = results.some(r => r.includes('❌')) ? '1px solid #f5c6cb' : '1px solid #c3e6cb';
                }

                testApiBtn.disabled = false;
                testApiBtn.textContent = '测试连接';
            }

            // 绑定测试按钮事件
            if (testApiBtn) {
                console.log('绑定API测试按钮事件');
                testApiBtn.addEventListener('click', testApiConnection);
            } else {
                console.log('未找到API测试按钮');
            }
        });
    </script>
</head>
<body>
    <!-- 简洁的头部 -->
    <header class="preview-header">
        <div class="header-content">
            <a href="/preview" class="app-title">彼知AI - 数据预览</a>
            <div class="header-actions">
                <a href="javascript:history.back()" class="btn">返回</a>
            </div>
        </div>
    </header>

    <!-- 主要内容区域 -->
    <main class="preview-main">
        <div class="content-container">
            <%- body %>
        </div>
    </main>

    <!-- 简洁的页脚 -->
    <footer class="preview-footer">
        <div class="footer-content">
            <div class="footer-left">
                © 2025 彼知AI. 仅供数据预览使用
            </div>
            <div class="footer-right">
                <!-- API选择器 -->
                <div class="api-selector">
                    <label class="api-label">API测试模式:</label>
                    <select id="apiMode" class="api-select">
                        <option value="remote" selected>远程API (7882端口)</option>
                    </select>
                    <button type="button" id="testApiBtn" class="api-test-btn">测试连接</button>
                    <div id="apiStatus" class="api-status"></div>
                </div>
            </div>
        </div>
    </footer>
</body>
</html>
